<template>
  <div class="prediction-analysis">
    <h1>预测分析</h1>
    <PredictionInput @prediction-complete="handlePredictionComplete" />
    <div class="content-wrapper">
      <div class="main-content">
        <div class="prediction-panel" v-if="currentPrediction">
          <h2>市场预测 - {{ currentStockCode }}</h2>
          <div class="prediction-metrics">
            <div class="metric">
              <h3>预测价格</h3>
              <div class="value">{{ currentPrediction.prediction_value.toFixed(2) }}</div>
            </div>
            <div class="metric">
              <h3>信心指数</h3>
              <div class="value">{{ (currentPrediction.confidence * 100).toFixed(2) }}%</div>
            </div>
          </div>
          <PredictionChart 
            :stock-code="currentStockCode" 
            title="价格走势预测" 
            :prediction-data="currentPrediction"
          />
        </div>
        
        <div class="market-sentiment">
          <h2>市场情绪</h2>
          <div class="sentiment-overview">
            <div class="sentiment-score">
              <h3>整体情绪指数</h3>
              <div class="value">{{ formatPercentage(marketSentiment.overall) }}</div>
            </div>
            <div class="sector-sentiment">
              <h3>行业情绪</h3>
              <div v-for="(score, sector) in marketSentiment.sectors" :key="sector" class="sector-item">
                <span class="sector-name">{{ sectorNameMap[sector] || sector }}</span>
                <div class="sentiment-bar">
                  <div class="bar-fill" :style="{ width: `${score * 100}%` }"></div>
                </div>
                <span class="score">{{ formatPercentage(score) }}</span>
              </div>
            </div>
          </div>
        </div>
      </div>
      
      <div class="sidebar">
        <div class="trending-topics">
          <h2>热点话题</h2>
          <ul>
            <li v-for="topic in marketSentiment.trending_topics" :key="topic">
              {{ topic }}
            </li>
          </ul>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, onMounted } from 'vue'
import { useAITradeStore } from '@/store'
import PredictionChart from '@/components/prediction/PredictionChart.vue'
import PredictionInput from '@/components/prediction/PredictionInput.vue'

export default {
  name: 'PredictionAnalysis',
  components: {
    PredictionChart,
    PredictionInput
  },
  setup() {
    const store = useAITradeStore()
    const marketSentiment = ref({
      overall: 0,
      sectors: {},
      trending_topics: []
    })
    const currentPrediction = ref(null)
    const currentStockCode = ref('')

    const sectorNameMap = {
      technology: '科技',
      finance: '金融',
      consumer: '消费',
      healthcare: '医疗健康',
      energy: '能源'
    }

    const formatPercentage = (value) => {
      return `${(value * 100).toFixed(1)}%`
    }

    const fetchMarketSentiment = async () => {
      try {
        const result = await store.getMarketSentiment()
        if (result) {
          marketSentiment.value = result
        }
      } catch (error) {
        console.error('获取市场情绪数据失败:', error)
      }
    }

    const handlePredictionComplete = ({ stockCode, result }) => {
      currentStockCode.value = stockCode
      currentPrediction.value = result
    }

    onMounted(() => {
      fetchMarketSentiment()
    })

    return {
      marketSentiment,
      currentPrediction,
      currentStockCode,
      sectorNameMap,
      formatPercentage,
      handlePredictionComplete
    }
  }
}
</script>

<style scoped>
.prediction-analysis {
  padding: 20px;
}

.content-wrapper {
  display: flex;
  gap: 20px;
  margin-top: 20px;
}

.main-content {
  flex: 1;
}

.sidebar {
  width: 300px;
}

.prediction-panel, .market-sentiment, .trending-topics {
  background: var(--surface-background, #ffffff);
  border-radius: 8px;
  padding: 20px;
  margin-bottom: 20px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.prediction-metrics {
  display: flex;
  gap: 20px;
  margin: 20px 0;
}

.metric {
  flex: 1;
  text-align: center;
  padding: 15px;
  background: var(--surface-secondary, #f5f5f5);
  border-radius: 6px;
}

.metric h3 {
  margin: 0 0 10px 0;
  font-size: 0.9em;
  color: var(--text-secondary, #666);
}

.metric .value {
  font-size: 1.8em;
  font-weight: bold;
  color: var(--text-primary, #333);
}

.sentiment-overview {
  margin-top: 15px;
}

.sentiment-score {
  margin-bottom: 20px;
  text-align: center;
}

.sentiment-score .value {
  font-size: 2em;
  font-weight: bold;
  color: var(--accent-color, #4CAF50);
}

.sector-sentiment {
  margin-top: 15px;
}

.sector-item {
  display: flex;
  align-items: center;
  margin-bottom: 10px;
}

.sector-name {
  width: 80px;
  font-size: 0.9em;
}

.sentiment-bar {
  flex: 1;
  height: 8px;
  background: var(--surface-secondary, #f0f0f0);
  border-radius: 4px;
  margin: 0 10px;
  overflow: hidden;
}

.bar-fill {
  height: 100%;
  background: var(--accent-color, #4CAF50);
  border-radius: 4px;
  transition: width 0.3s ease;
}

.score {
  width: 60px;
  text-align: right;
  font-size: 0.9em;
}

.trending-topics ul {
  list-style: none;
  padding: 0;
  margin: 0;
}

.trending-topics li {
  padding: 10px;
  margin: 5px 0;
  background: var(--surface-secondary, #f5f5f5);
  border-radius: 4px;
  font-size: 0.9em;
}

h1 {
  margin: 0 0 20px 0;
  font-size: 1.8em;
  color: var(--text-primary, #333);
}

h2 {
  margin: 0 0 15px 0;
  font-size: 1.4em;
  color: var(--text-primary, #333);
}

h3 {
  margin: 0 0 10px 0;
  font-size: 1.1em;
  color: var(--text-secondary, #666);
}
</style>